package com.link.base.base.workplan.service;

import com.link.base.user.model.CoreUser;
import com.link.base.base.workplan.dao.mybatis.mapper.GuideWorkplanMapper;
import com.link.base.base.workplan.dao.mybatis.mapper.WorkplanMapper;
import com.link.base.base.workplan.model.Workplan;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.DateUtil;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 任务计划
 */
@Component
public class WorkplanServiceImpl extends BasicServiceImpl<Workplan> implements WorkplanService {

    @Resource
    private WorkplanMapper workplanMapper;
    @Resource
    private GuideWorkplanMapper guideWorkplanMapper;

    @Override
    public BasicMapper<Workplan> getBasicMapper() {
        return workplanMapper;
    }

    @Override
    public List<Workplan> getPlanList(Workplan t) {
        return workplanMapper.getPlanList(t);
    }

    @Override
    public Map<String, Workplan> queryAllByMonth(Workplan entity) throws Exception {
        Map<String, Workplan> result = new HashMap<>();

        String inputDateStr = entity.getFilterParm2();
        // 传入参数日期
        Date inputDate = DateUtil.stringToDate(inputDateStr, "yyyy-MM-dd");

        // 查询整月的全部任务

        CoreUser user = UserUtil.getUser();
        entity.setLogin(user.getLogin());
        entity.setPageFlag(false);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        entity.setFilterParm2(simpleDateFormat.format(inputDate));

        List<Workplan> workplans = queryByExamplePage(entity);

        if (workplans != null && workplans.size() > 0) {
            // 根据传入的月份,获取当月的所有日期
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            List<String> daysList = DateUtil.getAllDaysOfMonth(inputDate);
            for (String date : daysList) {
                Workplan temp = new Workplan();
                result.putIfAbsent(date, temp);
                for (Workplan workplan : workplans) {
                    // 用当月任务做遍历,与每一天做比较
                    if (simpleDateFormat.parse(workplan.getBegin_time()).getTime() <= simpleDateFormat.parse(date).getTime()
                            && simpleDateFormat.parse(workplan.getEnd_time()).getTime() >= simpleDateFormat.parse(date).getTime()) {
                        // 如果结果集中已经存在日期,则将任务数量加1
                        Integer taskNum = result.get(date).getTaskNum() + 1;
                        temp.setTaskNum(taskNum);
                    }
                }
            }
        }
        return result;
    }

    @Override
    public Workplan queryById(Workplan entity) throws Exception {
        if (GUIDE_APP.equals(entity.getLoginType())) {
            return guideWorkplanMapper.queryById(entity);
        } else {
            return workplanMapper.queryById(entity);
        }
    }

    @Override
    public List<Workplan> queryByExamplePage(Workplan entity) throws Exception {
        if (GUIDE_APP.equals(entity.getLoginType())) {
            return guideWorkplanMapper.queryByExamplePage(entity);
        } else {
            return workplanMapper.queryByExamplePage(entity);
        }
    }
}
